import requests
import os
# requests模块, 也能拿来下东西. 使用它的另一种方法, 我们可以更加有效率的下载大文件.
os.makedirs('./img/',exist_ok=True)
image_url = "https://morvanzhou.github.io/static/results/scraping/3-3-1.png"
r = requests.get(image_url)
with open('./img/image2.png','wb') as f:
    f.write(r.content)
"""
一个 chunk 一个 chunk 的下载. 使用 r.iter_content(chunk_size) 来控制每个 chunk 的大小, 然后在文件中写入这个 chunk 大小的数据.
"""
r2 = requests.get(image_url, stream=True)    # stream loading

with open('./img/image3.png', 'wb') as f:
    for chunk in r2.iter_content(chunk_size=32):
        f.write(chunk)
